package com.cxy.client;

import com.cxy.factory.Factory;
import com.cxy.factory.HuaweiFactory;
import com.cxy.factory.XiaomiFactory;
import com.cxy.phone.Phone;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FactoryMethodClient {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//        Phone phone = null;
//        HuaweiFactory huaweiFactory = new HuaweiFactory();
//        Phone huawei = huaweiFactory.producePhone();
//        huawei.call("10086");
//
//        XiaomiFactory xiaomiFactory = new XiaomiFactory();
//        Phone xiaomi = xiaomiFactory.producePhone();
//        xiaomi.call("10086");

        //通过反射实例化具体的工厂
        Class<?> aClass = Class.forName("com.cxy.factory." + args[0]);
        Method producePhone = aClass.getMethod("producePhone");
        Constructor<?> constructor = aClass.getConstructor();
        Factory factory = (Factory) constructor.newInstance();
        Phone invoke = (Phone) producePhone.invoke(factory);
        invoke.call("10068");

    }
}
